package com.wwu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.LoginLog;
import com.wwu.system.mapper.LoginLogMapper;
import com.wwu.system.service.ILoginLogService;
import com.wwu.system.vo.QueryVO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 * 系统配置服务实现类
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {
    @Resource
    private LoginLogMapper loginLogMapper;


    /**
     * 分页查询系统登陆日志信息
     * @param pageNum  页码
     * @param pageSize  每页多少条
     * @param loginLog   系统登陆日志实体
     * @param queryVO   前端查询条件封装实体类
     * @return com.wwu.common.web.ResultPage
     * @author 一蓑烟雨
     * @date 2023/5/31 20:50
     */
    @Override
    public ResultPage getLoginLoglistPage(Integer pageNum, Integer pageSize, LoginLog loginLog, QueryVO queryVO) {
        Page<LoginLog> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<LoginLog> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(loginLog.getUserName())){
            wrapper.like(LoginLog::getUserName,loginLog.getUserName());
        }
        if(StringUtils.hasText(loginLog.getSessionId())){
            wrapper.like(LoginLog::getSessionId,loginLog.getSessionId());
        }
        if(StringUtils.hasText(loginLog.getLoginIp())){
            wrapper.like(LoginLog::getLoginIp,loginLog.getLoginIp());
        }
        if(null != loginLog.getLoginStatus()){
            wrapper.eq(LoginLog::getLoginStatus,loginLog.getLoginStatus());
        }
        if(StringUtils.hasText(queryVO.getStartDate()) && StringUtils.hasText(queryVO.getEndDate())){
            wrapper.between(LoginLog::getLoginTime,queryVO.getStartDate(),queryVO.getEndDate());
        }
        wrapper.orderByDesc(LoginLog::getRecordId);
        IPage<LoginLog> loginLogPage = this.page(page, wrapper);
        return new ResultPage<>(loginLogPage);
    }

    /**
     * 异步保存登陆日志
     * @param loginLog 登陆日志实体
     * @author 一蓑烟雨
     * @date 2023/7/29 11:16
     */
    @Async
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveLoginLog(LoginLog loginLog) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(loginLog),"登陆日志不能为空");
        AssertUtil.isTrue(!this.save(loginLog),"登陆日志保存失败！");
    }

    /**
     * 异步更新登陆日志(退出时间登)
     * @param loginLog 登陆日志实体
     * @author 一蓑烟雨
     * @date 2023/7/28 11:38
     */
    @Async
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateLoginLog(LoginLog loginLog){
        AssertUtil.isTrue(ObjectUtils.isEmpty(loginLog),"登陆日志不能为空");
        AssertUtil.isTrue(loginLogMapper.updateLoginLog(loginLog)<=0,"登陆日志更新失败");
    }

    /**
     * 删除登陆日志信息
     * @param recordIds 记录id
     * @author 一蓑烟雨
     * @date 2023/7/27 19:40
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteLoginLog(List<String> recordIds) {
        if(!ObjectUtils.isEmpty(recordIds)){
            AssertUtil.isTrue(!this.removeByIds(recordIds),"登陆日志删除失败！");
        }else{
            AssertUtil.isTrue(!this.remove(new LambdaQueryWrapper<>()),"登陆日志清空失败");
        }
    }

    /**
     * 定时任务删除登陆日志
     * @param loginLog  登陆日志实体
     * @param queryVO 查询条件
     * @return int
     * @author 一蓑烟雨
     * @date 2023/8/16 16:18
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int deleteLoginLog(LoginLog loginLog, QueryVO queryVO) {
        LambdaQueryWrapper<LoginLog> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(loginLog.getSessionId())){
            wrapper.eq(LoginLog::getSessionId,loginLog.getSessionId());
        }
        if(StringUtils.hasText(queryVO.getStartDate()) && StringUtils.hasText(queryVO.getEndDate())){
            wrapper.between(LoginLog::getLoginTime,queryVO.getStartDate(),queryVO.getEndDate());
        }

        int count  = this.count(wrapper);
        //如果数量大于0则执行删除操作
        if(count > 0){
            if(this.remove(wrapper)){
                return count;
            }else{
                return -1;
            }
        }
        return count;
    }
}
